use pom::parser::*;
use std::iter::FromIterator;
fn space<'a>() -> Parser<'a, char, ()> {
    one_of(" \t\r\n").repeat(0..).discard()
}
fn param<'a>() -> Parser<'a, char, String> {
    not_a(|c| c == ',' || c == ')')
        .repeat(1..)
        .map(|v: Vec<char>| String::from_iter(v))
}
fn name<'a>() -> Parser<'a, char, String> {
    not_a(|c| c == '(')
        .repeat(1..)
        .map(|v: Vec<char>| String::from_iter(v))
}
fn array<'a>() -> Parser<'a, char, Vec<String>> {
    let elems = list(param(), sym(',') * space());
    space() * sym('(') * space() * elems - sym(')')
}
fn exp<'a>() -> Parser<'a, char, (String, Vec<String>)> {
    let vals = space() * name() + array() - space();
    vals.map(|(col, paras)| (col, paras))
}
pub(crate) fn parse_calc_exp(exp_str: &str) -> anyhow::Result<(String, Vec<String>)> {
    let input: Vec<char> = exp_str.chars().collect();
    let v = exp().parse(&input)?;
    Ok(v)
}
